﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;


namespace Job.Core
{
    public class BaseAccessHelper
    {
        /// <summary>
        /// 获取连接字符串名称，使用企业库的数据层连接数据库
        /// </summary>
        /// <param name="dbEnum"></param>
        /// <returns></returns>
        public static string getConnName(DataBaseEnum dbEnum)
        {
            string _connName = dbEnum.ToString();
            //switch (dbEnum)
            //{
            //    case DataBaseEnum.AppMyGoodJob:
            //        _connName = GetSqlConnectionString(dbEnum);
            //        break;
            //    default:
            //        break;
            //}
            return _connName;
        }

        /// <summary>
        /// 获取sql连接字符串 （connectionStrings节）
        /// </summary>
        /// <param name="dbEnum"></param>
        /// <returns></returns>
        public static string GetSqlConnectionString(DataBaseEnum dbEnum)
        {
            return ConfigurationManager.ConnectionStrings[getConnName(dbEnum)].ConnectionString;
        }

        /// <summary>
        /// 获取MongoDB连接字符串 （appSettings节）
        /// </summary>
        /// <param name="dbEnum"></param>
        /// <returns></returns>
        public static string getMongoDBConnectionString(DataBaseEnum dbEnum)
        {
            return ConfigurationManager.AppSettings[dbEnum.ToString()].ToString();
        }

        /// <summary>
        /// 将Type类型转换为DbType
        /// </summary>
        /// <param name="sysType"></param>
        /// <returns></returns>
        public static DbType GetDbType(Type sysType)
        {
            DbType dbType = DbType.String;
            switch (sysType.Name)
            {
                case "String":
                    dbType = DbType.String;
                    break;
                case "Int16":
                    dbType = DbType.Int16;
                    break;
                case "Int32":
                    dbType = DbType.Int32;
                    break;
                case "Int64":
                    dbType = DbType.Int64;
                    break;
                case "DateTime":
                    dbType = DbType.DateTime;
                    break;
                case "Double":
                    dbType = DbType.Double;
                    break;
                case "Boolean":
                    dbType = DbType.Boolean;
                    break;
                default:
                    break;
            }
            return dbType;
        }

        /// <summary>
        /// 获取查询结构语句
        /// </summary>
        /// <param name="sysType"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static string GetQueryVal(Type sysType, object val)
        {
            string strVal = string.Empty;

            switch (sysType.Name)
            {
                case "AnsiString":
                case "Date":
                case "DateTime":
                case "Guid":
                case "Object":
                case "String":
                case "Time":
                case "AnsiStringFixedLength":
                case "StringFixedLength":
                case "Xml":
                case "DateTime2":
                case "DateTimeOffset":
                    strVal = string.Format("{0}", val);
                    strVal = string.IsNullOrEmpty(strVal) ? string.Empty : strVal.Replace("'", "''");
                    strVal = string.Format("'{0}'", strVal);
                    break;
                case "Binary":
                case "Byte":
                case "Boolean":
                case "Currency":
                case "Decimal":
                case "Double":
                case "Int16":
                case "Int32":
                case "Int64":
                case "SByte":
                case "Single":
                case "UInt16":
                case "UInt32":
                case "UInt64":
                case "VarNumeric":
                    strVal = string.Format("{0}", val);
                    break;
                default:
                    strVal = string.Format("{0}", val);
                    break;
            }
            return strVal;
        }

        /// <summary>
        /// 格式化字符串为 SQL 语句字段
        /// </summary>
        /// <param name="fldList"></param>
        /// <returns></returns>
        public static string GetSQLFildList(string strColumnList)
        {
            if (string.IsNullOrEmpty(strColumnList) || strColumnList == "*")
                return "*";
            else
            {
                return strColumnList;

                strColumnList = strColumnList.Replace("[", "").Replace("]", "");
                List<string> listColumnList = strColumnList.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList<string>();
                StringBuilder sb = new StringBuilder();
                foreach (var obj in listColumnList)
                {
                    string str = obj.Trim();
                    if (str == "*" || str.IndexOf(" as ") != -1)
                    {
                        sb.AppendFormat("{0},", str);
                    }
                    else
                    {
                        sb.AppendFormat("[{0}],", str);
                    }
                }
                return sb.ToString().Substring(0, sb.ToString().Length - 1);
            }
        }
    }
}
